package vboot.core.module.mon.online.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.web.bind.annotation.*;
import vboot.core.common.api.R;
import vboot.core.common.dao.JdbcDao;
import vboot.core.config.dbx.redis.RedisHandler;
import vboot.core.security.pojo.Zuser;
import vboot.core.xutil.ruoyi.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("mon/online/user")
public class MonOnlineUserApi {

    @Autowired
    private RedisHandler redisHandler;

    @GetMapping("list")
    public R getList() {
        List<String> keyList = redisHandler.scan("online-key*");
        Collections.reverse(keyList);
        String keys="(";
        for (String key : keyList) {
            keys+="'"+key+"',";
        }
        keys = keys.substring(0, keys.length() - 1) + ")";
        String sql="select * from mon_log_login where onkey in "+keys+" order by crtim desc";
        List<OnlineUserDto> list = jdbcDao.getTp().query(sql,
                new BeanPropertyRowMapper<>(OnlineUserDto.class));
        return R.ok(list);
    }

    @PostMapping("close")
    public R postClose(String onkey) {
        redisHandler.del(onkey);
        return R.ok();
    }




    @Autowired
    private JdbcDao jdbcDao;

}
